using System.Collections.Generic;
using Net.Common;
using UnityEngine;
using UnityEngine.UI;

/****************************************************
// 功能：
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/3/26 11:18:9
*****************************************************/

public class HeroSelectWindow : BaseWindow
{
    private Image imgHero;
    private Text txtConfirm;
    private Transform transScrollRoot;
    private GameObject heroItem;
    private Button btnSure;
    private Transform transSkillIconRoot;
    private List<HeroSelectData> heroSelectList;
    private float currentTime = Constants.NUM_THIRTY;
    private int time;
    private bool isSelected;
    private int selectHeroId;

    private void Awake()
    {
        
        imgHero = FindChildTransform(transform, "imgHero").GetComponent<Image>();
        txtConfirm = FindChildTransform(transform, "txtConfirm").GetComponent<Text>();
        transScrollRoot = FindChildTransform(transform, "Content").GetComponent<Transform>();
        btnSure = FindChildTransform(transform, "btnSure").GetComponent<Button>();
        transSkillIconRoot = FindChildTransform(transform, "SkillRoot").GetComponent<Transform>();
    }
    
    private void Update()
    {
        currentTime -= Time.deltaTime;
        if (time <= currentTime)
        {
            SetText(txtConfirm, Tool.ConvertSecondsToTime((int) currentTime));
        }
        else
        {
            // 倒计时结束，默认选择第一个英雄
            OnConfirmButton();
        }
    }
    
    
    protected override void InitWindows()
    {
        base.InitWindows();
        
        isSelected = false;
        time = Constants.NUM_ZERO;
        heroSelectList = model.userInfo.heroSelectData;
        
        btnSure.onClick.AddListener(OnConfirmButton);
        
        for(var i = transScrollRoot.childCount - 1; i >= 0; --i) {
            DestroyImmediate(transScrollRoot.GetChild(i).gameObject);
        }
        
        for(var i = 0; i < heroSelectList.Count; i++) {
            var heroId = heroSelectList[i].heroId;
            var go = resource.LoadPrefab("Prefab/ItemHero", transform, true);
            go.name = heroId.ToString();
            go.transform.SetParent(transScrollRoot);
            go.transform.localPosition = Vector3.zero;
            go.transform.localScale = Vector3.one;
            UnitConfig cfg = resource.GetUnitConfigData(heroId);
            SetSprite(go.transform.Find("imgIcon").GetComponent<Image>(), "Images/select/" + cfg.resName + "_head");
            SetText(go.transform.Find("txtName").GetComponent<Text>(), cfg.unitName);

            OnClick(go, OnHeroItemClick, go, heroId);
            
            // 默认选择第一个英雄
            if(i == 0) { OnHeroItemClick(new object[] {go, heroId}); }
        }
    }

    /// <summary>
    /// 选择英雄
    /// </summary>
    /// <param name="args"></param>
    private void OnHeroItemClick(object[] args)
    {
        audioManager.PlayUIAudio("selectHeroClick");
        if (isSelected)
        {
            ui.ShowTips("已经选定了英雄");
            return;
        }
        var game = args[0] as GameObject;
        
        for(var i = 0; i < transScrollRoot.childCount; i++) {
            var item = transScrollRoot.GetChild(i);
            var selectGlow = item.Find("state").GetComponent<Image>();
            SetSprite(selectGlow, item.gameObject.Equals(game) ? "Images/select/selectGlow" : "Images/match/frame_normal");
        }

        selectHeroId = (int) args[1];
        var cfg = resource.GetUnitConfigData(selectHeroId);
        SetSprite(imgHero, "Images/select/" + cfg.resName + "_show");
        
        for(var i = 0; i < transSkillIconRoot.childCount; i++)
        {
            var icon = transSkillIconRoot.GetChild(i).GetComponent<Image>();
            SetSprite(icon, "Images/player/" + cfg.resName + "_sk" + i);
        }
    }

    
    private void OnConfirmButton()
    { 
        audioManager.PlayUIAudio("com_click2");
        if (isSelected) { return; }
        
        SendByTcp(RequestType.SELECT_HERO, new HeroRequest()
        {
            roomId = model.roomId,
            heroId = selectHeroId,
            type = (int) RequestType.SELECT_HERO
        });

        btnSure.interactable = false;
        isSelected = true;
    }
}
